using AutoMapper;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Domain.Entities;
using UwinEducation.Domain.IRepositories;
using UwinEducation.Shared.Models;


namespace UwinEducation.Application.Services
{
    /// <summary>
    /// 角色服务实现
    /// </summary>
    public class RoleService : IRoleService
    {
        private readonly IRoleRepository _roleRepository;
        private readonly IRolePermissionRepository _rolePermissionRepository;
        private readonly IMapper _mapper;

        public RoleService(IRoleRepository roleRepository, IRolePermissionRepository rolePermissionRepository, IMapper mapper)
        {
            _roleRepository = roleRepository;
            _rolePermissionRepository = rolePermissionRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取角色分页列表
        /// </summary>
        public async Task<PagedResult<RoleDto>> GetPagedListAsync(PageRequest pageRequest)
        {
            var result = await _roleRepository.GetPagedListAsync(pageRequest);
            return new PagedResult<RoleDto>
            {
                Total = result.Total,
                Items = _mapper.Map<List<RoleDto>>(result.Items)
            };
        }

        /// <summary>
        /// 获取角色详情
        /// </summary>
        public async Task<RoleDto> GetByIdAsync(Guid id)
        {
            var role = await _roleRepository.GetByIdAsync(id);
            return _mapper.Map<RoleDto>(role);
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        public async Task<RoleDto> CreateAsync(CreateRoleDto input)
        {


            if(await _roleRepository.ExistsByNameAsync(input.Name))
            {
                throw new ArgumentException($"角色名已存在: {input.Name}");
            }

            var role = _mapper.Map<Role>(input);
            await _roleRepository.CreateAsync(role);
            return _mapper.Map<RoleDto>(role);
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        public async Task UpdateAsync(UpdateRoleDto input)
        {
            var role = await _roleRepository.GetByIdAsync(input.Id);
            _mapper.Map(input, role);
            await _roleRepository.UpdateAsync(role);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        public async Task DeleteAsync(Guid id)
        {
            await _roleRepository.DeleteAsync(id);
        }

        /// <summary>
        /// 分配菜单权限
        /// </summary>
        public async Task AssignMenusAsync(AssignMenusDto input)
        {
            await _roleRepository.AssignMenusAsync(input.RoleId, input.PermissionCodes);
        }
        public async Task<List<string>> GetPermissionCodesAsync(Guid roleId)
        {
            // 检查角色是否存在
            var role = await _roleRepository.GetByIdAsync(roleId);
            if (role == null)
            {
                throw new ArgumentException($"角色不存在: {roleId}");
            }
            var roleInfos = await _rolePermissionRepository.GetRoleCodesByRoleIdAsync(role.Id);
            return roleInfos;
        }

    }
}